package com.lgq.cfw.controller.permission;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lgq.cfw.bean.TableAdvertisement;
import com.lgq.cfw.bean.message.Message;
import com.lgq.cfw.service.AdvertisementService;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import net.sf.jsqlparser.schema.Table;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Lin
 * @date 2019-02-14 20:36
 **/
@RestController
@RequestMapping(value = "/permission/advertisement")
public class AdvertisementController {

  @Autowired
  AdvertisementService advertisementService;

  /**
   * 获取所有信息
   * @param currentPage 当前页面
   * @return 处理信息
   */
  @RequestMapping("/getAll")
  public Message getAll(@RequestParam(value="currentPage",defaultValue="1")Integer currentPage){

    PageHelper.startPage(currentPage, 10);
    List<TableAdvertisement> tableAdvertisementList;
    try {
      tableAdvertisementList = advertisementService.getAll();
    } catch (Exception e) {
      return Message.fail("处理失败",null);
    }
    PageInfo<TableAdvertisement> pageInfo = new PageInfo<>(tableAdvertisementList,5);
    return Message.success("处理成功",null).add("pageInfo",pageInfo);
  }

  /**
   * 添加广告
   * @param name 广告名
   * @param multipartFile 广告图片
   * @param session 获取路径
   * @return 返回信息
   */
  @RequestMapping(value = "/add",method = RequestMethod.POST)
  public Message add(@RequestParam(value = "advertisement")MultipartFile multipartFile,String name
      , HttpSession session){
    //1.获取某个文件夹在服务器上的真实路径
    ServletContext servletContext=session.getServletContext();
    //2.使用context对象获取真实路径
    String advertisementPath=servletContext.getRealPath("/advertisements");
    //3.把文件上传到这个位置
    String fileName= UUID.randomUUID().toString().replace("-","").substring(0,10)+"_file_"
        +multipartFile.getOriginalFilename();
    System.out.println(fileName);
    //文件上传后的网路位置
    String netUrl="advertisements/"+fileName;
    try {
      multipartFile.transferTo(new File(advertisementPath+"/"+fileName));
      System.out.println(advertisementPath+fileName);
    } catch (IOException e) {
      e.printStackTrace();
    }
    TableAdvertisement tableAdvertisement=new TableAdvertisement();
    tableAdvertisement.setName(name);
    tableAdvertisement.setUrl(netUrl);
    tableAdvertisement.setStatus("0");
    //这里需要添加userId
    boolean flag=advertisementService.add(tableAdvertisement);
    if (flag){
      return Message.success("处理成功",null);
    }else {
      return Message.fail("处理失败",null);
    }
  }

  /**
   * 删除广告
   * @param advertisementDeleteIdString id字符串
   * @return 处理信息
   */
  @RequestMapping(value = "/delete/{advertisementDeleteIdString}",method = RequestMethod.DELETE)
  public Message delete(@PathVariable(value = "advertisementDeleteIdString")String advertisementDeleteIdString){
    boolean flag=advertisementService.delete(advertisementDeleteIdString);
    if (flag){
      return Message.success("处理成功",null);
    }else{
      return Message.fail("处理失败",null);
    }
  }

  /**
   * 更新广告
   * @param multipartFile 图片文件
   * @param tableAdvertisement 疯转的广告
   * @param session 获取地址
   * @return 处理信息
   */
  @RequestMapping(value = "/update",method = RequestMethod.POST)
  public Message update(@RequestParam(value = "advertisement")MultipartFile multipartFile
      ,TableAdvertisement tableAdvertisement, HttpSession session) {
    ServletContext servletContext = session.getServletContext();
    //2.使用context对象获取真实路径
    String advertisementPath = servletContext.getRealPath("/advertisements");
    //3.把文件上传到这个位置
    String fileName = UUID.randomUUID().toString().replace("-", "").substring(0, 10) + "_file_"
        + multipartFile.getOriginalFilename();
    System.out.println(fileName);
    //文件上传后的网路位置
    String netUrl = "advertisements/" + fileName;
    try {
      multipartFile.transferTo(new File(advertisementPath + "/" + fileName));
      System.out.println(advertisementPath + fileName);
    } catch (IOException e) {
      e.printStackTrace();
    }
    tableAdvertisement.setUrl(netUrl);
    //这里需要添加userId
    boolean flag = advertisementService.update(tableAdvertisement);
    if (flag) {
      return Message.success("处理成功", null);
    } else {
      return Message.fail("处理失败", null);
    }
  }

}
